package aom.maozj.net.forward.desthost;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;

import javax.net.ServerSocketFactory;

public class NetListenServer {
	private int port;

	public NetListenServer(int port) {
		this.port = port;
	}

	public void start(INetListenServerCallback callback) throws IOException {
		ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(port);
		new Thread(() -> {
			byte buffer[] = new byte[1024];
			int len;
			while (true) {
				try {
					Socket socket = serverSocket.accept();
					InputStream in = socket.getInputStream();
					len = in.read(buffer);
					if (len > 3) {
						if (Arrays.equals(Arrays.copyOfRange(buffer, 0, 3), "SSH".getBytes())) { // ssh request

						} else if (Arrays.equals(Arrays.copyOfRange(buffer, 0, 3), "Ser".getBytes())) { // trans provider
							TransProvider provider = new TransProvider();
						} else { // unknow
							socket.close();
						}
					} else {
						socket.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}, "master net listen server @" + port).start();
		serverSocket.accept();
	}
}

class Connection {
	private Socket socket;

	public Connection(Socket socket) {
		// TODO Auto-generated constructor stub
		this.socket = socket;
	}
}

interface INetListenServerCallback {
	void acceptTransRequest(TransRequestor requestor);

	void acceptTransProvide(TransProvider provider);

}
